<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>  
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Wild Fire Learning Solutions</title>
  <link href="css/style.css" rel="stylesheet" type="text/css" />
  <script src="datafeed.js" type="text/javascript"></script>

</head>


<body>
<div id="contain">
	<?php include("header.php"); ?>
<!--Navigation and LHS-->
	<div id="leftcol">
		<?php include("menu.php"); ?>
	</div>

<!-- Nav ends -->
	
<!-- Content (RHS) -->
	<div id="content">
		<p>
		<?php
		session_start();
		//############################################
		//Now import the settings
		include_once("settings.php");
		
		//############################################
		//Connect to the db
		mysql_connect($db_host,$MySqlUN,$MySqlPW);
		@mysql_select_db($database) or die( "Unable to select databases");
		
		//Let's make the variables easy to access in our queries
			$firstname = $_POST['firstname'];
			$lastname = $_POST['lastname'];
			$add1 = $_POST['add1'];
			$add2 = $_POST['add2'];
			$city = $_POST['city'];
			$state = $_POST['state'];
			$zip= $_POST['zip'];
			$phone = $_POST['phone'];
			$fax = $_POST['fax'];
			$email = $_POST['email'];
			$shipfirst =$_POST['shipfirst'];
			$shiplast = $_POST['shiplast'];
			$shipadd1 = $_POST['shipadd1'];
			$shipadd2 = $_POST['shipadd2'];
			$shipcity = $_POST['shipcity'];
			$shipstate = $_POST['shipstate'];
			$shipzip = $_POST['shipzip'];
			$shipphone = $_POST['shipphone'];
			$shipemail = $_POST['shipemail'];
			$total = $_POST['total'];
			$sessid = session_id();
			$today = date("Y-m-d");
			
			// 1) Assign Customer Number to new Customer, or find existing customer number
			$query = "select * from customers where 
					(customers_firstname = '$firstname' AND
					customers_lastname = '$lastname' AND
					customers_add1 = '$add1' AND
					customers_add2 = '$add2' AND
					customers_city = '$city')";
			$results = mysql_query($query) or die(mysql_error());
			$rows = mysql_num_rows($results);
			if($rows < 1) {
				//assign new custnum
				$query2 = "insert into customers (
		customers_firstname,customers_lastname,customers_add1,customers_add2,customers_city,customers_state,customers_zip,customers_phone,customers_fax,customers_email)
				values(
				'$firstname',
				'$lastname',
				'$add1',
				'$add2',
				'$city',
				'$state',
				'$zip',
				'$phone',
				'$fax',
				'$email')";
		$insert = mysql_query($query2) or die(mysql_error());
		$custid = mysql_insert_id();
		}
		
		//If custid exists, we want to make it equal to custnum
		//Otherwise we will use the existing custnum
		if($custid) {
		$customers_custnum = $custid;
		}
		// 2)Insert Info into ordermain
		//determine shipping costs based on order total(25% of total)
		$shipping = $total * 0.25;
		
		$query3 = "insert into ordermain (
				  ordermain_orderdate,
				  ordermain_custnum,
				  ordermain_subtotal,
				  ordermain_shipping,
				  ordermain_shipfirst,
				  ordermain_shiplast,
				  ordermain_shipadd1,
				  ordermain_shipadd2,
				  ordermain_shipcity,
				  ordermain_shipstate,
				  ordermain_shipzip,
				  ordermain_shipphone,
				  ordermain_shipemail)
				  values(
				  '$today',
				  '$customers_custnum',
				  '$total',
				  '$shipping',
				  '$shipfirst',
				  '$shiplast',
				  '$shipadd1',
				  '$shipadd2',
				  '$shipcity',
				  '$shipstate',
				  '$shipzip',
				  '$shipphone',
				  '$shipemail')";
		
		$insert2 = mysql_query($query3) or die(mysql_error());
		$orderid = mysql_insert_id();
		// 3) Insert Info into orderdet
		//find the correct cart information being temporarily stored
		$query ="select * from carttemp where carttemp_sess = '$sessid'";
		$results = mysql_query($query) or die(mysql_error());
		
		//put the data into the database one row at a time
		while($row = mysql_fetch_array($results)) {
		extract($row);
		$query4 = "insert into orderdet (
					orderdet_ordernum,orderdet_qty,orderdet_prodnum)
					values(
					'$orderid',
					'$carttemp_quan',
					'$carttemp_prodnum')";
		$insert4 = mysql_query($query4) or die(mysql_error());
		}
		// 4) delete from temporary table
		$query = "delete from carttemp where carttemp_sess = '$sessid'";
		$delete = mysql_query($query);
		// 5) email confirmations to us and to the customer
		/* recipients */
		$to = "<" . $email . ">";
		/* subject */
		$subject = "Order Confirmation";
		/*message */
		/*top of message*/
		$message = "
		<html>
		<head>
		<table>Order Confirmation</title>
		</head>
		<body>
		Here is a recap of your order:<br><br>
		Order date: ";
		$message .= $today;
		$message .= "
		<br>
		Order Number:";
		$message .= $orderid;
		$message .= "
		<table width=\"50%\" border=\"0\">
			<tr>
			<td>
				<p>Bill to:<br>";
				$message .= $firstname;
				$message .= " ";
				$message .= $lastname;
				$message .= "<br>";
				$message .= $add1;
				$message .= "<br>";
		if($add2) {
				$message .= $add2 . "<br>";
		}
		$message .=$city . ", " . $state . " " . $zip;
		$message .="</p></td>
		<td>
		<p>Ship to:<br>";
		$message .= $shipfirst ." " .$shiplast;
		$message .="<br>";
		$message .=$shipadd1 ."<br>";
		if($shipadd2) {
		$message .= $shipadd2 . "<br>";
		}
		$message .=$shipcity . ", " .$shipstate . " " .$shipzip;
		$message .= "</p>
		</td>
		</tr>
		</table>
		<hr width=\"250px\" align=\"left\">
		<table cellpadding=\"5\">";
		
		//grab the contents of the order and insert then into the message field
		$query = "select * from orderdet where orderdet_ordernum = '$orderid'";
		$results = mysql_query($query) or die(mysql_error());
		while($row = mysql_fetch_array($results)) {
			extract($row);
			$prod = "select * from products where products_prodnum = '$orderdet_prodnum'";
			$prod2 = mysql_query($prod);
			$prod3 = mysql_fetch_array($prod2);
			extract($prod3);
			$message .="<tr><td>";
			$message .=$orderdet_qty;
			$message .="</td>";
			$message .="<td>";
			$message .=$products_name;
			$message .="</td>";
			$message .="<td align=\"right\">";
			$message .=$products_price;
			$message .="<td>";
			$message .="<td align=\"right\">";
			//get extended price
			$extprice = number_format($products_price * orderdet_qty,2);
			$message .= $extprice;
			$message .="</td>";
			$message .="</tr>";
		}
		
		$message .="<tr>
		<td colspan=\"3\" align=\"right\">
		Your total before shipping is:
		</td>
		<td align=\"right\">";
		$message .= number_format($total,2);
		$message .= "
		</td>
		<tr>
		<td colspan=\"3\" align=\"right\">
		Shipping Costs:
		</td>
		<td align=\"right\">";
		$message .= number_format($shipping,2);
		$message .= "
		</td>
		</tr>
		<tr>
		<td colspan=\"3\" align=\"right\">
		Your final total is:
		</td>
		<td align=\"right\">";
		$message .= number_format(($total + $shipping),2);
		$message .= "
		</td>
		</tr>
		</table>
		</body>
		</html>";
		/* headers */
		$headers = "MIME-Version:1.0\r\n";
		$headers .= "Contant-type:text/html; charset=iso-8859-1\r\n";
		$headers .= "From:<kolianuja@gmail.com>\r\n";
		$headers .="Cc:<shrithatsme@gmail.com>\r\n";
		$headers .="X-Mailer:PHP / " .phpversion() . "\r\n";
		
		/* mail it*/
		mail($to, $subject,$message,$headers);
		
		// 6) show them list order & give them an order number
		
		echo "Step 1 - Please Enter Billing and Shipping Information<br>";
		echo "Step 2 - Please Verify Accuracy and Make Any Necessary Changes<br>";
		echo "<strong>Step 3 - Order Confirmation and Receipt</strong<br><br>";
		echo $message;
		
		?>


		</p>
	</div>
<!-- content ends (RHS) -->
<?php include("footer.php"); ?>

</div>
</body>
</html>